<?php
//安装程序

@set_time_limit(1000);
header('Content-Type: text/html; charset=utf-8');
require dirname(__FILE__).'/../include/common.inc.php';

$action = isset($webrequest['action']) ? $webrequest['action'] : '';
$sql = isset($webrequest['sql']) ? $webrequest['sql'] : '';
if ($action == 'ok')
{
	echo '恭喜您，安装成功。&nbsp;<a href="install.php">[返回安装首页]</a>';
	exit();
}
elseif ($action == 'do')
{
	if ($sql == '')
	{
		exit('error');
	}
	global_ConnectDB();
	$strSql = file_get_contents($sql);
	$strSql = str_replace("\r", "\n", $strSql);
	$strSql = preg_replace("/(^|\n)(\-{2}|#)(.*)/", "\n", $strSql);
	$tempSqlArray = explode(";\n", $strSql);
	foreach ($tempSqlArray as $tempSql)
	{
		$tempSql = trim($tempSql);
		if ($tempSql != '')
		{
			$db->Query($tempSql);
		}
	}
	echo "<script type=\"text/javascript\">".
		 "function redirect() {window.location.replace('install.php?action=ok');}\n".
		 "setTimeout('redirect();', 2000);\n".
		 "</script>";
	echo '正在安装 '.$sql.' ...';
	exit();
}
else
{
	$current_dir = @opendir('.');
	while ($entryname = @readdir($current_dir))
	{
		if (is_file($entryname) && strtolower(substr($entryname, -4))=='.sql')
		{
			echo '<p>数据文件：'.$entryname.'&nbsp;&nbsp;<a href="install.php?action=do&sql='.urlencode($entryname).'">[点击安装]</a></p>';
		}
	}
	@closedir($current_dir);
	exit();
}

?>